/**
 * Created by scutterry on 15/4/20.
 *
 * version 1.1, send 函数添加 dt 参数，控制 ajax 数据格式，默认为 json
 * 
 */
define(['jquery', 'data_const', 'text!views/loading.html'], function($, DC, html){
    function Net(){
        this.loading = $(html);
        this.list = [];
        this.currentObj;
        this.isBusy = false;
    }
    Net.prototype.post = function (url, data, callback, thisArg, dt){
        if(!dt){
            dt = "json";
        }

        $("body").append(this.loading);
        var self = this;

        // headers: {
        //     "Access-Control-Allow-Origin":"*",
        //     "Access-Control-Allow-Headers":"X-Requested-With"
        // }
        $.ajax({
            "url": url,
            "data": data,
            "type": "POST",
            "dataType": dt,
            "success": function(res){
                console.log("recv data", res);
                // if(res.code < 0){
                //     if(res.code === -1){
                //         res.func = callback;
                //         res.thisArg = thisArg;
                //         var param = {};
                //         if(res.data){
                //             param.data = res.data.data;
                //             param.url = res.data.url;
                //         }
                //         param.func = callback;
                //         param.thisArg = thisArg;
                //         $(document).trigger(DC.EVENT_SHOW_LOGIN, param);
                //     }

                // }else{
                    if(callback != null){
                        callback.call(thisArg, res);
                    }
                // }

                self.loading.remove();
            },

            "error": function(res){
                if(res.readyState == 0 || res.status == 0)return;
                if(typeof data == "object"){
                    res = JSON.stringify(res);
                }
                self.loading.remove();
                alert("ajax 请求错误" + res);
            }
        });
        return this;
    };

    Net.prototype.get = function (url, callback, thisArg){
        $("body").append(this.loading);
        var self = this;
        console.log(url);

        // headers: {
        //     "Access-Control-Allow-Origin":"*",
        //     "Access-Control-Allow-Headers":"X-Requested-With"
        // }
        $.ajax({
            url: url,
            type: "GET",
            success: function(res){
                console.log("recv data", res);
                if(res.code < 0){
                    if(res.code === -1){
                        res.func = callback;
                        res.thisArg = thisArg;
                        var param = {};
                        if(res.data){
                            param.data = res.data.data;
                            param.url = res.data.url;
                        }
                        param.func = callback;
                        param.thisArg = thisArg;
                        $(document).trigger(DC.EVENT_SHOW_LOGIN, param);
                    }

                }else{
                    if(callback != null){
                        callback.call(thisArg, res);
                    }
                }
                self.loading.remove();
            },

            error: function(data){
                if(data.readyState == 0 || data.status == 0)return;
                var res;
                if(typeof data == "object"){
                    res = JSON.stringify(data);
                }else {
                    res = data;
                }
                self.loading.remove();
                alert("ajax 请求错误" + res);
            }
        });
        return this;
    };

    var instance;
    Net.getInstance = function(){
        if(!instance){
            instance = new Net();
        }
        return instance;
    };

    return Net;
});